﻿using Entitas;
using System.Linq;
class EnemyPursueTargetRequestSystem : IExecuteSystem
{
    EnemyContext enemyContext;
    RequestContext requestContext;
    public EnemyPursueTargetRequestSystem(Contexts contexts)
    {
        enemyContext = contexts.enemy;
        requestContext = contexts.request;
    }
    public void Execute()
    {
        foreach (var e in enemyContext.GetEntities().Where(x => x.hasTargetInformation && x.isBattleEngaged == true))
        {
            var newposition = e.position.value;
            var attackposition = new EntitasVec3();
            var target = Contexts.sharedInstance.player.GetEntityWithName(e.targetInformation.Name);

            //enemy is to the right of target... move to the left
            if (e.position.value.x > target.position.value.x)
                attackposition.x = target.position.value.x + 2f;
            else attackposition.x = target.position.value.x - 2f;

            //same height level as target
            attackposition.y = target.position.value.y;

            if (e.position.value != attackposition)
            {
                if (e.position.value.x > attackposition.x && e.position.value.y == attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Left);

                }
                else if (e.position.value.x > attackposition.x && e.position.value.y < attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Up_Left);

                }

                else if (e.position.value.x > attackposition.x && e.position.value.y > attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Down_Left);

                }

                else if (e.position.value.x < attackposition.x && e.position.value.y == attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Right);

                }
                else if (e.position.value.x < attackposition.x && e.position.value.y < attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Up_Right);

                }
                else if (e.position.value.x < attackposition.x && e.position.value.y > attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Down_Right);

                }

                else if (e.position.value.x == attackposition.x && e.position.value.y > attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Down);

                }
                else if (e.position.value.x == attackposition.x && e.position.value.y < attackposition.y)
                {
                    requestContext.CreateEntity().AddBattleMovementRequest(e.name.value, Direction.Up);

                }
                else ServiceManager.DebugMessageService.Print("in the correct attackpositon");

                
            }
        }
    }
}


